package com.weisy.signin.ui.activity;

import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.squareup.picasso.Picasso;
import com.weisy.signin.R;
import com.weisy.signin.adapter.CommAdapter;
import com.weisy.signin.base.BaseAcativity;
import com.weisy.signin.bean.MsgEvent;
import com.weisy.signin.bean.Sign;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.utils.ImageUtils;
import com.weisy.signin.widget.CircleTransform;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author weisy
 * @function 签到列表
 * @date 2019/3/22 22:23
 */
@Route(path = Comm.ROUTE_SIGN_LIST)
public class SignListActivity extends BaseAcativity {
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.line_no_data)
    LinearLayout lineNull;
    @BindView(R.id.list_sign)
    ListView lisySign;

    private ArrayList<Sign> signs = new ArrayList<>();
    private CommAdapter<Sign> adapter;

    @Override
    public void findView() {
        setContentView(R.layout.activity_sign_list);
        ARouter.getInstance().inject(this);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initView() {
        adapter = new CommAdapter<Sign>(signs, R.layout.layout_item_mage_stu) {
            @Override
            public void bindView(ViewHolder holder, Sign obj) {
                if (!TextUtils.isEmpty(obj.getImgUrl())) {
                    Bitmap bitmap = ImageUtils.str2Bitmap(obj.getImgUrl());
                    Picasso.get().load(ImageUtils.bit2Uri(context, bitmap)).error(R.drawable.ic_head_default)
                            .placeholder(R.drawable.ic_head_default).transform(new CircleTransform()).into((ImageView) holder.getView(R.id.img_stu_head));
                }
                holder.setText(R.id.txt_stu_item1, obj.getName());
                holder.setText(R.id.txt_stu_item2, obj.getStudentId());
                holder.setText(R.id.txt_stu_item3, obj.getSignTime());
            }
        };
        lisySign.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(refreshLayout -> {
            initData();
        });
    }

    @Override
    public void initData() {
        signs.clear();
        List<Sign> tmp = LitePal.findAll(Sign.class);
        if (null != tmp && !tmp.isEmpty()) {
            signs.addAll(tmp);
            adapter.notifyDataSetChanged();
        }
        if (signs.isEmpty()) {
            lineNull.setVisibility(View.VISIBLE);
        } else {
            lineNull.setVisibility(View.GONE);
        }
        new Handler().postDelayed(() -> refreshLayout.finishRefresh(), 1000);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(MsgEvent msgEvent) {
        switch (msgEvent.getCode()) {
            case Comm.SIGN_INFO:
                Sign sign = (Sign) msgEvent.getData();
                ToastUtils.show(sign.getName() + "已签到");
                initData();
                break;
        }
    }

    @OnClick(R.id.img_list_close)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_list_close:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
